<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: AggregatorService.cfc 1003 2006-12-20 20:30:23Z mpwoodward $

Notes:
--->
<cfcomponent
	displayname="AggregatorService"
	output="false"
	hint="AggregatorService for MachBlog">

	<!---
	PROPERTIES
	--->
	<cfproperty name="aggregatorGateway" type="AggregatorGateway" required="true" />
	<cfproperty name="aggregatorDAO" type="AggregatorDAO" required="true" />
	<cfproperty name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
	<cfproperty name="resourceBundleFacade" type="org.machblog.facades.ResourceBundleFacade" required="true" />
	<cfset variables.instance = StructNew() />

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" output="false" returntype="AggregatorService" 
		hint="Initializes the service.">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setDevMode" access="public" returntype="void" output="false">
		<cfargument name="devMode" required="true" type="string" />
		<cfset variables.instance.devMode = arguments.devMode />
	</cffunction>
	<cffunction name="getDevMode" access="private" returntype="string" output="false">
		<cfreturn variables.instance.devMode />
	</cffunction>
	
	<cffunction name="getAggregatorBean" access="public" output="false" returntype="Aggregator">
		<cfreturn createObject("component", "Aggregator").init() />
	</cffunction>

	<cffunction name="setAggregatorGateway" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="aggregatorGateway" type="AggregatorGateway" required="true" />
		<cfset variables.aggregatorGateway = arguments.aggregatorGateway />
	</cffunction>	
	<cffunction name="getAggregatorGateway" access="private" output="false" returntype="AggregatorGateway">
		<cfreturn variables.aggregatorGateway />
	</cffunction>
	
	<cffunction name="setAggregatorDAO" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="aggregatorDAO" type="AggregatorDAO" required="true" />
		<cfset variables.aggregatorDAO = arguments.aggregatorDAO />
	</cffunction>
	<cffunction name="getAggregatorDAO" access="private" output="false" returntype="AggregatorDAO">
		<cfreturn variables.aggregatorDAO />
	</cffunction>
	
	<cffunction name="setUserFacade" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>	
	<cffunction name="getUserFacade" access="private" output="false" returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>

	<cffunction name="setResourceBundleFacade" access="public" output="false" returntype="void" 
			hint="Dependency: injected">
		<cfargument name="resourceBundleFacade" type="org.machblog.facades.ResourceBundleFacade" required="true" />
		<cfset variables.resourceBundleFacade = arguments.resourceBundleFacade />
	</cffunction>
	<cffunction name="getResourceBundleFacade" access="private" output="false" returntype="org.machblog.facades.ResourceBundleFacade">
		<cfreturn variables.resourceBundleFacade />
	</cffunction>
	
	<!---
	GATEWAY RELATED
	--->
	<cffunction name="getAllAggregators" access="public" output="false" returntype="query" 
			hint="Returns a query object containing all aggregators (active and inactive)">
		<cfreturn getAggregatorGateway().getAggregators() />
	</cffunction>
	
	<cffunction name="getActiveAggregators" access="public" output="false" returntype="query" 
			hint="Returns a query object containing active aggregators">
		<cfreturn getAggregatorGateway().getAggregators(getActiveOnly = true) />
	</cffunction>
	
	<cffunction name="getAutoPingAggregators" access="public" output="false" returntype="query" 
			hint="Returns a query object containing aggregators set to be auto-pinged">
		<cfreturn getAggregatorGateway().getAggregators(getAutoPingOnly = true) />
	</cffunction>
	
	<cffunction name="pingAggregators" access="public" output="false" returntype="void" 
			hint="Pings the aggregators set to be auto-pinged">
		<cfset var aggregators = getAutoPingAggregators() />
		
		<cfif aggregators.RecordCount GT 0>
			<cfloop query="aggregators">
				<cfhttp method="get" url="#aggregators.ping_url#" resolveurl="false" />
				
				<!--- update the ping date --->
				<cfset updateLastPing(aggregators.aggregator_id) />
			</cfloop>
		</cfif>
	</cffunction>
	
	<cffunction name="pingAggregator" access="public" output="false" returntype="void" 
			hint="Pings a single aggregator">
		<cfargument name="aggregatorID" type="uuid" required="true" />
		
		<cfset var aggregator = getAggregator(arguments.aggregatorID) />
		
		<cfhttp method="get" url="#aggregator.getPingUrl()#" resolveurl="false" />
		<cfset updateLastPing(aggregator.getAggregatorId()) />
	</cffunction>
	
	<!---
	DAO RELATED
	--->
	<cffunction name="createAggregator" access="public" output="false" returntype="void" hint="Creates an aggregator">
		<cfargument name="aggregator" type="Aggregator" required="true" />
		
		<cfif arguments.aggregator.getAggregatorId() EQ "">
			<cfset arguments.aggregator.setAggregatorId(CreateUUID()) />
		</cfif>
		
		<cfset getAggregatorDAO().create(arguments.aggregator) />
	</cffunction>
	
	<cffunction name="getAggregator" access="public" output="false" returntype="struct"
		hint="Retrieves an aggregator by ID">
		<cfargument name="aggregatorId" type="string" required="true" />
		
		<cfset var getAggregator = "" />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		<cfset var dtModified = getResourceBundleFacade().getLocaleUtils().toEpoch(createDate(1900, 1, 1)) />
		<cfset var dtLastPinged = getResourceBundleFacade().getLocaleUtils().toEpoch(createDate(1900, 1, 1)) />
		<cfset var aggregator = getAggregatorBean() />
		
		<cfset getAggregator = getAggregatorDAO().read(arguments.aggregatorId) />
		
		<!--- If we got a hit, populate the object --->
		<cfif getAggregator.recordCount EQ 1>
			<cfset cbName = getAggregator.cbfn & " " & getAggregator.cbln />
			
			<!--- handle the modified by stuff --->
			<cfif getAggregator.modified_by_id NEQ "">
				<cfset mbName = getAggregator.mbfn & " " & getAggregator.mbln />
				<cfset dtModified = getResourceBundleFacade().getLocaleUtils().fromEpoch(getAggregator.dt_modified) />
			</cfif>
			
			<cfif getAggregator.dt_last_pinged NEQ "">
				<cfset dtLastPinged = getResourceBundleFacade().getLocaleUtils().fromEpoch(getAggregator.dt_last_pinged) />
			</cfif>
				
			<cfset aggregator.init(getAggregator.aggregator_id, getAggregator.aggregator_name, 
											getAggregator.aggregator_description, getAggregator.aggregator_url, 
											getAggregator.ping_url, getAggregator.auto_ping, 
											dtLastPinged) />
			<cfset aggregator.getAudit().init(getAggregator.created_by_id, cbName, getAggregator.dt_created, 
													getAggregator.ip_created, getAggregator.modified_by_id, mbName, 
													dtModified, getAggregator.ip_modified, getAggregator.is_active) />
		</cfif>

		<cfreturn aggregator />
	</cffunction>
	
	<cffunction name="updateAggregator" access="public" output="false" returntype="void" hint="Updates an aggregator">
		<cfargument name="aggregator" type="Aggregator" required="true" />

		<cfset getAggregatorDAO().update(arguments.aggregator) />
	</cffunction>
	
	<cffunction name="updateLastPing" access="public" output="false" returntype="void" 
			hint="Updates an aggregator's 'last pinged' field">
		<cfargument name="aggregatorId" type="uuid" required="true" />
		<cfset getAggregatorDAO().updateLastPing(arguments.aggregatorId) />
	</cffunction>
	
	<cffunction name="deleteAggregator" access="public" output="false" returntype="void" hint="Updates an aggregator">
		<cfargument name="aggregator" type="Aggregator" required="true" />

		<cfset getAggregatorDAO().delete(arguments.aggregator) />
	</cffunction>
</cfcomponent>